﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using log4net;
using LeakView.service;
using GMap.NET.WindowsForms;
using LeakView.common;
using GMap.NET.EXT.providers;
using GMap.NET;
using GMap.NET.WindowsForms.Markers;

namespace LeakView.view
{
    public partial class ViewMapForHistory : UserControl
    {
        readonly StartEndOverlay startEndOverlay = new StartEndOverlay();
        readonly RouteOverlay routeOverlay = new RouteOverlay();
        readonly LeaksOverlay leaksOverlay = new LeaksOverlay();

        public ViewMapForHistory()
        {
            InitializeComponent();
        }

        private void ViewMapForHistory_Load(object sender, EventArgs e)
        {

            //Bing混合地图  
            GMapControl gMap = this.gMapControl1;
            if (!CSharpUtilities.IsDesignMode())
            {
                gMap.MapProvider = AMapProvider.Instance;
                gMap.CacheLocation = Application.StartupPath + "\\maps";
            }

            //访问模式
            int mode = AnyConfigs.get("APP").get("MapAccessMode").GetOrDefault(1);
            switch (mode)
            {
                case 0:
                    gMap.Manager.Mode = AccessMode.ServerOnly;
                    break;
                case 2:
                    gMap.Manager.Mode = AccessMode.CacheOnly;
                    break;
                default:
                    gMap.Manager.Mode = AccessMode.ServerAndCache;
                    break;
            }

            //不显示中心十字点  
            gMap.ShowCenter = false;

            //左键拖拽地图  
            gMap.DragButton = MouseButtons.Left;
            gMap.MinZoom = 1;   //最小缩放  
            gMap.MaxZoom = 18;  //最大缩放  

            gMap.Overlays.Add(routeOverlay);
            gMap.Overlays.Add(startEndOverlay);
            gMap.Overlays.Add(leaksOverlay);

            gMap.Zoom = Properties.Settings.Default.map_zoom;

            //地图中心位置  
            gMap.Position = PipeModal.ME.Center;  //中心点的纬度，经度   

        }

        public void showLeak(AjaxLeakInfo leak)
        {
            routeOverlay.show();
            startEndOverlay.show();
            leaksOverlay.show(leak);

            this.gMapControl1.Position = PipeModal.ME.Center;
        }


        class RouteOverlay : GMapOverlay
        {
            private GMapRoute route = null;

            public void show()
            {
                if(this.route != null)
                {
                    this.route.Clear();
                    this.route = null;
                }

                route = new GMapRoute(PipeModal.ME.Points, "line")
                {
                    Stroke = new Pen(Color.Chocolate, 4.0F)
                };
                this.Routes.Add(route);

                //this.Control.Position = PipeModal.ME.Center;
            }

        }

        class StartEndOverlay : GMapOverlay
        {
            readonly GMapMarker start = new GMarkerGoogle(PipeModal.default_center, GMarkerGoogleType.green_dot);
            readonly GMapMarker end = new GMarkerGoogle(PipeModal.default_center, GMarkerGoogleType.blue_dot);

            public StartEndOverlay()
            {
                start.ToolTipText = AppConfig.HeadName;
                start.ToolTipMode = MarkerTooltipMode.OnMouseOver;
                start.IsVisible = false;
                Markers.Add(start);

                end.ToolTipText = AppConfig.TailName;
                end.ToolTipMode = MarkerTooltipMode.OnMouseOver;
                end.IsVisible = false;
                Markers.Add(end);

            }

            public void show()
            {
                try
                {
                    if(MainModule.pair.Value.Valid)
                    {
                        start.Position = PipeModal.ME.First;
                        start.IsVisible = true;
                        start.ToolTipText = MainModule.pair.Value.Value.head.name;
                        end.Position = PipeModal.ME.Last;
                        end.IsVisible = true;
                        end.ToolTipText = MainModule.pair.Value.Value.tail.name;
                    }
                    else
                    {
                        start.IsVisible = false;
                        end.IsVisible = false;
                    }

                }
                catch
                {

                }

            }

        }

        class LeaksOverlay : GMapOverlay
        {
            public LeaksOverlay()
            {
            }

            public void show(AjaxLeakInfo leak)
            {
                this.Markers.Clear();

                if(leak != null)
                {
                    PointLatLng pos = PipeModal.ME.GetPosition(leak.location);
                    GMapMarker leakMarker = new GMarkerGoogle(pos, GMarkerGoogleType.red_pushpin)
                    {
                        ToolTipText = leak.location.ToString("F3") + "千米",
                        ToolTipMode = MarkerTooltipMode.OnMouseOver,
                        Tag = leak
                    };
                    this.Markers.Add(leakMarker);
                }

            }

        }
    }
}
